/*
 * directoryTreeTest.cpp
 *
 *  Created on: 2012-8-26
 *      Author: fan
 */

#include "stdlib.h"
#include "stdio.h"
#include "directory_tree.h"

#include "..\support\test\test.h"
#include "..\support\common\string\wchar_string.h"


static State BuildFileTreeNodeTest()
{
    const Wchar* pTestString = L"fileName";
    DirTreeFileNode* pTestFileNode = BuildFileTreeNode(pTestString);
    if ((DirTreeFileNode*)NULL == pTestFileNode)
        return FAILD;

    return ProcessDirectoryTreeFileNodes(pTestFileNode);
}


static State DestoryFileTreeNodeTest()
{
    const Wchar* pTestString = L"fileName";
    DirTreeFileNode* pTestFileNode = BuildFileTreeNode(pTestString);
    if ((DirTreeFileNode*)NULL == pTestFileNode)
        return FAILD;
    DestoryFileTreeNode(pTestFileNode);
    return SUCCESS;

}


static State BuildDirectoryNodeTest()
{
    const Wchar* pTestString = L"directoryName";
    DirTreeDirectoryNode* pTestDirectoryNode = BuildDirectoryNode(pTestString);
    if ((DirTreeDirectoryNode*)NULL == pTestDirectoryNode)
        return FAILD;
    return ProcessDirectoryTreeDirectoryNodes(pTestDirectoryNode);
}


static State DestoryDirectoryNodeTest()
{
    const Wchar* pTestString = L"directoryName";
    DirTreeDirectoryNode* pTestDirectoryNode = BuildDirectoryNode(pTestString);
    if ((DirTreeDirectoryNode*)NULL == pTestDirectoryNode)
        return FAILD;
    DestoryDirectoryNode(pTestDirectoryNode);
    return SUCCESS;
}


static State InsertDirectoryNodeTest()
{
    const Wchar* pRootDirectoryName = L"RootdirectoryName";
    DirTreeDirectoryNode* pTestRootDirectoryNode = BuildDirectoryNode(pRootDirectoryName);
    if ((DirTreeDirectoryNode*)NULL == pTestRootDirectoryNode)
        return FAILD;

    const Wchar* pDirectoryName1 = L"directoryName1";
    DirTreeDirectoryNode* pTestDirectoryNode1 = BuildDirectoryNode(pDirectoryName1);
    if ((DirTreeDirectoryNode*)NULL == pTestDirectoryNode1)
        return FAILD;

    InsertDirectoryNode(pTestRootDirectoryNode, pTestDirectoryNode1);

    const Wchar* pDirectoryName2 = L"directoryName2";
    DirTreeDirectoryNode* pTestDirectoryNode2 = BuildDirectoryNode(pDirectoryName2);
    if ((DirTreeDirectoryNode*)NULL == pTestDirectoryNode2)
        return FAILD;

    InsertDirectoryNode(pTestRootDirectoryNode, pTestDirectoryNode2);

    GetDirectoryTreeDirectoryNodes(pTestRootDirectoryNode);

    return SUCCESS;
}


static State DeleteDirectoryNodeTest()
{
    const Wchar* pRootDirectoryName = L"RootdirectoryName";
    DirTreeDirectoryNode* pTestRootDirectoryNode = BuildDirectoryNode(pRootDirectoryName);
    if ((DirTreeDirectoryNode*)NULL == pTestRootDirectoryNode)
        return FAILD;

    const Wchar* pDirectoryName1 = L"directoryName1";
    DirTreeDirectoryNode* pTestDirectoryNode1 = BuildDirectoryNode(pDirectoryName1);
    if ((DirTreeDirectoryNode*)NULL == pTestDirectoryNode1)
        return FAILD;
    InsertDirectoryNode(pTestRootDirectoryNode, pTestDirectoryNode1);

    const Wchar* pDirectoryName2 = L"directoryName2";
    DirTreeDirectoryNode* pTestDirectoryNode2 = BuildDirectoryNode(pDirectoryName2);
    if ((DirTreeDirectoryNode*)NULL == pTestDirectoryNode2)
        return FAILD;
    InsertDirectoryNode(pTestRootDirectoryNode, pTestDirectoryNode2);

    const Wchar* pDirectoryName3 = L"directoryName3";
    DirTreeDirectoryNode* pTestDirectoryNode3 = BuildDirectoryNode(pDirectoryName3);
    if ((DirTreeDirectoryNode*)NULL == pTestDirectoryNode3)
        return FAILD;
    InsertDirectoryNode(pTestRootDirectoryNode, pTestDirectoryNode3);

    const Wchar* pDirectoryName4 = L"directoryName4";
    DirTreeDirectoryNode* pTestDirectoryNode4 = BuildDirectoryNode(pDirectoryName4);
    if ((DirTreeDirectoryNode*)NULL == pTestDirectoryNode4)
        return FAILD;
 //   InsertDirectoryNode(pTestRootDirectoryNode, pTestDirectoryNode4);



    GetDirectoryTreeDirectoryNodes(pTestRootDirectoryNode);

  ////////////////////////////////////////////////////////////////////////////
    wprintf(L" now delete a directory Node 2\n");
    DeleteDirectoryNode(pTestRootDirectoryNode, pTestDirectoryNode2);
    DestoryDirectoryNode(pTestDirectoryNode2);
    GetDirectoryTreeDirectoryNodes(pTestRootDirectoryNode);

    wprintf(L" now delete a directory Node 1\n");
    DeleteDirectoryNode(pTestRootDirectoryNode, pTestDirectoryNode1);
    DestoryDirectoryNode(pTestDirectoryNode1);
    GetDirectoryTreeDirectoryNodes(pTestRootDirectoryNode);

    wprintf(L" now delete a directory Node 4\n");
    DeleteDirectoryNode(pTestRootDirectoryNode, pTestDirectoryNode4);
    DestoryDirectoryNode(pTestDirectoryNode4);
    GetDirectoryTreeDirectoryNodes(pTestRootDirectoryNode);

    wprintf(L" now delete a directory Node 3\n");
    DeleteDirectoryNode(pTestRootDirectoryNode, pTestDirectoryNode3);
    DestoryDirectoryNode(pTestDirectoryNode3);
    GetDirectoryTreeDirectoryNodes(pTestRootDirectoryNode);

    return SUCCESS;
}


static State InsertFileNodeTest()
{
    const Wchar* pDirectoryName = L"directoryName";
    DirTreeDirectoryNode* pTestDirectoryNode = BuildDirectoryNode(pDirectoryName);
    if ((DirTreeDirectoryNode*)NULL == pTestDirectoryNode)
        return FAILD;

    const Wchar* pFileName1 = L"fileName1";
    DirTreeFileNode* pTestFileNode1 = BuildFileTreeNode(pFileName1);
    if ((DirTreeFileNode*)NULL == pTestFileNode1)
        return FAILD;
    InsertFileNode(pTestDirectoryNode, pTestFileNode1);

    const Wchar* pFileName2 = L"fileName2";
    DirTreeFileNode* pTestFileNode2 = BuildFileTreeNode(pFileName2);
    if ((DirTreeFileNode*)NULL == pTestFileNode2)
        return FAILD;
    InsertFileNode(pTestDirectoryNode, pTestFileNode2);

    const Wchar* pFileName3 = L"fileName3";
    DirTreeFileNode* pTestFileNode3 = BuildFileTreeNode(pFileName3);
    if ((DirTreeFileNode*)NULL == pTestFileNode3)
        return FAILD;
    InsertFileNode(pTestDirectoryNode, pTestFileNode3);

    GetDirectoryTreeFileNodes(pTestDirectoryNode);

    return SUCCESS;
}

static State DeleteFileNodeTest()
{
    const Wchar* pDirectoryName = L"directoryName";
    DirTreeDirectoryNode* pTestDirectoryNode = BuildDirectoryNode(pDirectoryName);
    if ((DirTreeDirectoryNode*)NULL == pTestDirectoryNode)
        return FAILD;

    const Wchar* pFileName1 = L"fileName1";
    DirTreeFileNode* pTestFileNode1 = BuildFileTreeNode(pFileName1);
    if ((DirTreeFileNode*)NULL == pTestFileNode1)
        return FAILD;
    InsertFileNode(pTestDirectoryNode, pTestFileNode1);

    const Wchar* pFileName2 = L"fileName2";
    DirTreeFileNode* pTestFileNode2 = BuildFileTreeNode(pFileName2);
    if ((DirTreeFileNode*)NULL == pTestFileNode2)
        return FAILD;
    InsertFileNode(pTestDirectoryNode, pTestFileNode2);

    const Wchar* pFileName3 = L"fileName3";
    DirTreeFileNode* pTestFileNode3 = BuildFileTreeNode(pFileName3);
    if ((DirTreeFileNode*)NULL == pTestFileNode3)
        return FAILD;
    InsertFileNode(pTestDirectoryNode, pTestFileNode3);

    const Wchar* pFileName4 = L"fileName4";
    DirTreeFileNode* pTestFileNode4 = BuildFileTreeNode(pFileName4);
    if ((DirTreeFileNode*)NULL == pTestFileNode4)
        return FAILD;
 //   InsertFileNode(pTestDirectoryNode, pTestFileNode4);


    GetDirectoryTreeFileNodes(pTestDirectoryNode);

    ////////////////////////////////////////////////////////////////////////////
      wprintf(L" now delete a file Node 2 \n");
      DeleteFileNode(pTestDirectoryNode, pTestFileNode2);
      DestoryFileTreeNode(pTestFileNode2);
      GetDirectoryTreeFileNodes(pTestDirectoryNode);

      wprintf(L" now delete a file Node 1 \n");
      DeleteFileNode(pTestDirectoryNode, pTestFileNode1);
      DestoryFileTreeNode(pTestFileNode1);
      GetDirectoryTreeFileNodes(pTestDirectoryNode);

      wprintf(L" now delete a file Node 4 \n");
      DeleteFileNode(pTestDirectoryNode, pTestFileNode4);
      DestoryFileTreeNode(pTestFileNode4);
      GetDirectoryTreeFileNodes(pTestDirectoryNode);

      wprintf(L" now delete a file Node 3 \n");
      DeleteFileNode(pTestDirectoryNode, pTestFileNode3);
      DestoryFileTreeNode(pTestFileNode3);
      GetDirectoryTreeFileNodes(pTestDirectoryNode);

    return SUCCESS;
}


State DirectoryTreeTest()
{
    OUTPUT_FUNCTION_TEST_RESULT(BuildFileTreeNodeTest);

    OUTPUT_FUNCTION_TEST_RESULT(DestoryFileTreeNodeTest);

    OUTPUT_FUNCTION_TEST_RESULT(BuildDirectoryNodeTest);

    OUTPUT_FUNCTION_TEST_RESULT(DestoryDirectoryNodeTest);

    OUTPUT_FUNCTION_TEST_RESULT(InsertDirectoryNodeTest);

    OUTPUT_FUNCTION_TEST_RESULT(DeleteDirectoryNodeTest);

    OUTPUT_FUNCTION_TEST_RESULT(InsertFileNodeTest);

    OUTPUT_FUNCTION_TEST_RESULT(DeleteFileNodeTest);

    return SUCCESS;
}

